-- Decompiled with the Synapse X Luau decompiler.
--服务器单例 基类--维护玩家对象
local serverId=12125882338
local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.GameObject());
local workspace = workspace;
function M.ctor(self)
	M.super.ctor(self, workspace);
	self.player_list = {};--当前服务器玩家列表
	self.player_num = 0; --当前服务器玩家数量
end;
function M.dtor(self)
	M.super.dtor(self);
end;

local Players = game:GetService("Players");
local RunService = game:GetService("RunService");
local DataBaseManager = ServerOHOH.load.DataBaseManager();

--开启服务器
function M.Start(self)
	if self.OnStart then
		local v3, v4 = xpcall(self.OnStart, debug.traceback, self);
		if not v3 then
			warn(v4);
		end;
	end;
	for v5, playerInst in ipairs(Players:GetChildren()) do
		--warn("------------------------for v5, playerInst in ipairs(Players:GetChildren()) do-----------------------")
		self:AddProcess(self.PlayerInstIn, playerInst); --创建当前已进入服务器玩家（协程）
	end;
	Players.PlayerAdded:Connect(function(playerInst)
		--warn("----PlayerAdded---0----",playerInst.UserId)

		self:AddProcess(self.PlayerInstIn, playerInst);  --玩家进入事件绑定（协程）
	end);
	Players.PlayerRemoving:Connect(function(playerInst)
		--warn("---PlayerRemoving--0---",playerInst.UserId)

		self:AddProcess(self.PlayerInstOut, playerInst);--玩家离开事件绑定（协程）
	end);
	
--编辑下 特殊处理	
	if not RunService:IsStudio() then
		game:BindToClose(function()
			print("服务器关闭");
			while self.player_num > 0 or not (not DataBaseManager:IsSaving()) do
				print("wait for player dtor or database save", self.player_num, DataBaseManager:IsSaving());
				wait();			
			end;
			print("close server in ", os.clock() - os.clock(), "s");
		end);
	end;
	self:AddProcess(function()
		self:InitObjAsset(serverId)
	end)
end;

--角色是否自动加载
function M.SetAutoCreateCharacter(self, isAuto)
	Players.CharacterAutoLoads = isAuto;
end;
--获取角色是否加载
function M.GetAutoCrteateCharacter(self)
	return Players.CharacterAutoLoads;
end;

--玩家Inst进入
function M.PlayerInstIn(self, playerInst)
	--warn("------------------------function M.PlayerInstIn(self, playerInst)-----------------------")
	--warn("----PlayerAdded---1----",playerInst.UserId)

	---子类预处理
	if self.OnPlayerInstIn then
		local s, d = xpcall(self.OnPlayerInstIn, debug.traceback, self, playerInst);
		if not s then
			warn(d);
		end;
	end;
	--warn("----PlayerAdded---1.5----",playerInst.UserId)

	local player = nil;
	local s, d = xpcall(function()
		player = ServerOHOH.load.Server_Player().new(playerInst); -- 创建玩家对象
	end, debug.traceback);
	if not s then
		--warn("----PlayerAdded---1.4----",playerInst.UserId)

		warn(d);

		playerInst:Kick(d); --失败，提出玩家
	else
		--warn("----PlayerAdded---1.6----",playerInst.UserId)

		--玩家已初始化
		self:PlayerIn(player); --玩家对象进入
		
		--warn("----PlayerAdded---2----",playerInst.UserId)

	end;
end;

local Server_PayManager = ServerOHOH.load.Server_PayManager();
local InstanceManager = ServerOHOH.load.InstanceManager();

--玩家Inst离开
function M.PlayerInstOut(self, playerInst)
	--warn("---PlayerRemoving--1---",playerInst.UserId)

	if self.OnPlayerInstOut then
		local v11, v12 = xpcall(self.OnPlayerInstOut, debug.traceback, self, playerInst);
		if not v11 then
			warn(v12);
		end;
	end;
	--warn("---PlayerRemoving--1.5---",playerInst.UserId)

	Server_PayManager:DelPayingInfo(playerInst.UserId);
	local player = InstanceManager:FindObject(playerInst);
	if player then
		--warn("---PlayerRemoving--1.6---",playerInst.UserId)
		self:PlayerOut(player);
		--warn("---PlayerRemoving--2---",playerInst.UserId)
	else
		--warn("---PlayerRemoving--2.1---",playerInst.UserId)

	end;
end;
local ServerEventManager = ServerOHOH.load.ServerEventManager();
local ServerFunctionManager = ServerOHOH.load.ServerFunctionManager();
function M.PlayerIn(self, player)
	--warn("----PlayerAdded---1.7----",player.main_inst.UserId)

	-- 处理客户端已推送的函数和事件
	ServerEventManager:DelayGet(player);
	--warn("----PlayerAdded---1.8----",player.main_inst.UserId)

	ServerFunctionManager:DelayGet(player);
	--warn("----PlayerAdded---1.9----",player.main_inst.UserId)

	-- 缓存玩家对象，子类处理
	self.player_list[player] = true; 
	if self.OnPlayerIn then
		--warn("------PlayerIn------",player)
		self:OnPlayerIn(player);
		--warn("----PlayerAdded---1.91----",player.main_inst.UserId)

	end;
	--warn("----PlayerAdded---1.92----",player.main_inst.UserId)

	self.player_num = self.player_num + 1;
end;

function M.PlayerOut(self, player)
	--warn("----PlayerRemoving---1.7----",player.main_inst.UserId)

	if self.OnPlayerOut then
		--warn("----PlayerRemoving---1.8----",player.main_inst.UserId)

		self:OnPlayerOut(player);
		--warn("----PlayerRemoving---1.9----",player.main_inst.UserId)

	end;
	player:dtor();
	--warn("----PlayerRemoving---1.91----")

	self.player_list[player] = nil;
	self.player_num = self.player_num - 1;
end;

function M.GetPlayerList(self)
	return self.player_list 
end

--是否启用玩家鼠标锁定
local StarterPlayer = game:GetService("StarterPlayer");
function M.SetEnableMouseLockOption(self, isEnable)
	StarterPlayer.EnableMouseLockOption = isEnable;
end;

--获取玩家对象
function M.GetPlayerObjectByUserId(self, userId)
	return InstanceManager:FindObject((Players:GetPlayerByUserId(userId)));
end;

--获取系统玩家
function M.GetPlayerInstanceByUserId(self, userId)
	assert(userId, "param id is nil");
	return Players:GetPlayerByUserId(userId);
end;



return M;
